﻿Imports DotNetNuke.Entities

Public Class DDRProvider
	Implements DotNetNuke.Web.DDRMenu.INodeManipulator
	
	
	Const VentrianModuleName As String = "SimpleGallery"
	
	Public Function ManipulateNodes(nodes As System.Collections.Generic.List(Of DotNetNuke.Web.DDRMenu.MenuNode), portalSettings As DotNetNuke.Entities.Portals.PortalSettings) As System.Collections.Generic.List(Of DotNetNuke.Web.DDRMenu.MenuNode) Implements DotNetNuke.Web.DDRMenu.INodeManipulator.ManipulateNodes
		Dim toReturn As New List(Of DotNetNuke.Web.DDRMenu.MenuNode)
		
		Dim currentPortalId As Integer = portalSettings.PortalId
		Dim lstGaleryModules As New List(Of DotNetNuke.Entities.Modules.ModuleInfo)
		Dim mc As New DotNetNuke.Entities.Modules.ModuleController
		lstGaleryModules = (From myModule In mc.GetAllModules().Cast(Of DotNetNuke.Entities.Modules.ModuleInfo) _
			Where myModule.DesktopModule.ModuleName = VentrianModuleName).ToList
		Dim ac As New Ventrian.SimpleGallery.Entities.AlbumController
		For Each rootItem In nodes
			Dim currentNode As DotNetNuke.Web.DDRMenu.MenuNode=rootItem
			'get all galeries modules on the current page
			Dim lstModules As List(Of modules.moduleinfo) = lstGaleryModules.FindAll(Function(m As DotNetNuke.Entities.Modules.ModuleInfo) currentNode.TabId = m.TabID)
			If lstModules.Count > 0 Then
				For Each myModule In lstModules
					buildAlbumsNodes(currentNode,myModule.tabid,-1,ac.List(myModule.ModuleID, -1, True, True, Ventrian.SimpleGallery.Common.AlbumSortType.Caption, Ventrian.SimpleGallery.Common.SortDirection.ASC).Cast(Of Ventrian.SimpleGallery.Entities.AlbumInfo).tolist)
				Next
            End If
            If currentNode.Children.Count > 0 Then
                buildChildNodes(rootItem.Children, lstGaleryModules, ac)
            End If
            toReturn.Add(currentNode)
        Next
		Return toReturn
	End Function

    Private Sub buildChildNodes(ByVal lstNodes As List(Of DotNetNuke.Web.DDRMenu.MenuNode), ByVal lstGaleryModules As List(Of DotNetNuke.Entities.Modules.ModuleInfo), ByVal ac As Ventrian.SimpleGallery.Entities.AlbumController)
        For Each rootitem In lstNodes

            Dim currentNode As DotNetNuke.Web.DDRMenu.MenuNode = rootitem
            If Not currentNode.Children Is Nothing AndAlso currentNode.Children.Count > 0 Then
                buildChildNodes(currentNode.Children, lstGaleryModules, ac)
            End If
            Dim lstModules As List(Of Modules.ModuleInfo) = lstGaleryModules.FindAll(Function(m As DotNetNuke.Entities.Modules.ModuleInfo) currentNode.TabId = m.TabID)
            If lstModules.Count > 0 Then
                For Each myModule In lstModules
                    buildAlbumsNodes(currentNode, myModule.TabID, -1, ac.List(myModule.ModuleID, -1, True, True, Ventrian.SimpleGallery.Common.AlbumSortType.Caption, Ventrian.SimpleGallery.Common.SortDirection.ASC).Cast(Of Ventrian.SimpleGallery.Entities.AlbumInfo).ToList)
                Next
            End If
          

        Next
    End Sub

    Private Sub buildAlbumsNodes(ByRef currentNode As DotNetNuke.Web.DDRMenu.MenuNode, ByVal tabId As Integer, ByVal parentAlbumId As Integer, ByVal lstAlbums As List(Of Ventrian.SimpleGallery.Entities.AlbumInfo))

        Dim filteredLstAlbums As List(Of Ventrian.SimpleGallery.Entities.AlbumInfo) = (From currentAlbum In lstAlbums Where currentAlbum.ParentAlbumID = parentAlbumId).ToList()

        For Each myAlbum In filteredLstAlbums
            Dim myNewNode As New DotNetNuke.Web.DDRMenu.MenuNode()
            With myNewNode
                .Parent = currentNode
                .Text = myAlbum.Caption
                .Title = myAlbum.Description
                .Url = DotNetNuke.Common.NavigateURL(tabId, "", String.Format("AlbumId={0}-{1}", myAlbum.ModuleID, myAlbum.AlbumID))
                .TabId = tabId
                .Enabled = True
            End With
            buildAlbumsNodes(myNewNode, tabId, myAlbum.AlbumID, lstAlbums)
            currentNode.Children.Add(myNewNode)

        Next
    End Sub
End Class